package com.example.demo.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.ArticleVisitRecordDao;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 文章访问记录消息消费
 */
@Component
public class ArticleVisitConsumer {

    @Resource
    RedisTemplate redisTemplate;
    @Resource
    ArticleVisitRecordDao articleVisitRecordDao;

    private static DateFormat df =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,sss");

    /**
     * 把接收到的数据异步写入redis中
     */
    @JmsListener(destination = "ArticleDetailVisitQueue", containerFactory = "queueListener")
    public void ArticleDetailVisitQueueMessage(String message){
        redisTemplate.opsForList().rightPush("articleVisit", message);
    }

    /**
     * 把访客量数据批量写入数据库中
     * @param message
     */
    @JmsListener(destination = "batchInsertArticleVisitQueue", containerFactory = "queueListener")
    public void batchInsertArticleVisitQueue(String message){
        JSONArray msList = JSON.parseArray(message);

        // 获取当前日期
        String thisDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        // 设置批量写入DB的列表
        List batchInsert = new ArrayList();
        for (Object item : msList){
            // 把数据强转为JSONObject
            JSONObject msDetail = (JSONObject) item;
            HashMap<String, Object> insertData = new HashMap<String, Object>();
            insertData.put("ip", msDetail.get("clientIp"));
            insertData.put("article_id", msDetail.get("articleId"));
            insertData.put("visit_at", msDetail.get("visitDateTime"));
            insertData.put("create_at", thisDate);
            insertData.put("update_at", thisDate);
            batchInsert.add(insertData);
        }
        articleVisitRecordDao.batchInsert(batchInsert);

    }

    /**
     * destination 目标地址即队列
     */
    @JmsListener(destination = "ArticleVisitQueue", containerFactory = "queueListener")
    public void ArticleVisitQueueMessage(String text){
        System.out.println("队列一：接收队列消息时间：" + df.format(new Date()) + ", 接收到消息内容:" + text);
    }

    /**
     * destination 目标地址即队列
     */
    @JmsListener(destination = "ArticleVisitQueue", containerFactory = "queueListener")
    public void ArticleVisitQueueMessage2(String text){
        System.out.println("队列二：接收队列消息时间：" + df.format(new Date()) + ", 接收到消息内容:" + text);
    }


    /**
     * destination 目标地址即队列
     */
    @JmsListener(destination = "ArticleVisitTopic", containerFactory = "topicListener")
    public void ArticleVisitMessage(String text){
        System.out.println("广播一：接收队列消息时间：" + df.format(new Date()) + ", 接收到消息内容:" + text);
    }

    /**
     * destination 目标地址即队列
     * containerFactory 消息模式
     */
    @JmsListener(destination = "ArticleVisitTopic", containerFactory = "topicListener")
    public void ArticleVisitMessage2(String text){
        System.out.println("广播二：接收队列消息时间：" + df.format(new Date()) + ", 接收到消息内容:" + text);
    }

}
